import pandas as pd
from datetime import datetime

'''
 TIME: 200227172513
^[[?25l^[[0m^[[H^[[J^[[s^[[999C^[[999B^[[6n^[[uTasks: 574 total,   4 running, 566 sleeping,   0 stopped,   0 zombie            ^M
Mem:   2896740k total,  2879784k used,    16956k free,    27884k buffers        ^M
Swap:  1048572k total,    32764k used,  1015808k free,  1108148k cached         ^M
800%cpu 169%user   0%nice 249%sys 367%idle   0%iow  12%irq   2%sirq   0%host    ^M
^[[7m  PID USER         PR  NI VIRT  RES  SHR S[%CPU] %MEM     TIME+ ARGS            ^[[0m^M
 2762 system       20   0 2.0G 110M 110M S  126   3.8 637:01.41 com.askey.dvr.d+^M
  576 cameraserver  0 -20 377M  56M  17M S  114   1.9 1502:53.9 android.hardwar+^M
  996 mediacodec   20   0 176M  10M  10M S 51.0   0.3  31:03.93 media.codec hw/+^M
18754 root         20   0  12M 3.8M 3.0M R 48.9   0.1   0:00.29 top -n 1^M
  969 cameraserver 20   0  50M  13M  13M S 24.4   0.4 317:58.43 cameraserver^M
  587 system       20   0  72M 4.3M 4.3M S 12.2   0.1 168:13.72 android.hardwar+^M
 1601 system       18  -2 4.4G 137M 137M S  8.1   4.8 118:00.06 system_server^M
  258 root         -3   0    0    0    0 S  8.1   0.0  34:40.91 [kgsl_worker_th+^M
18562 root         20   0    0    0    0 S  4.0   0.0   0:08.12 [kworker/u16:3]^M
 2608 system       20   0 3.5G  68M  57M R  4.0   2.4  63:28.14 com.askeycloud.+^M
  549 root         20   0    0    0    0 S  4.0   0.0  45:09.21 [ipc_rtr_q6_ipc+^M
   32 root         20   0    0    0    0 S  4.0   0.0   0:30.67 [rcuop/2]^M
18441 root         20   0    0    0    0 S  2.0   0.0   0:00.96 [kworker/5:1]^M
18431 root         20   0    0    0    0 S  2.0   0.0   0:18.38 [kworker/u16:5]^M
18430 root         20   0    0    0    0 S  2.0   0.0   0:17.41 [kworker/u16:2]^M
 2872 system       20   0 3.5G  73M  53M S  2.0   2.5  30:24.68 com.askey.dvr.a+^M
 2415 root         RT   0    0    0    0 S  2.0   0.0  26:16.95 [sugov:0]^M
  616 audioserver  20   0  89M  17M  13M S  2.0   0.6  25:31.86 audioserver^M
  572 audioserver  20   0  45M  14M 6.6M S  2.0   0.4  30:32.51 android.hardwar+^M
   60 root         20   0    0    0    0 S  2.0   0.0   0:52.15 [rcuos/5]^M^[[?25h^[[0m^[[1000;1H^[[K^[[?25h^[[0m^[[1000;1H^[[K
'''


def cal_cpu_usage(data):
    items = data.split()
    idle = items[4].split('%')[0]
    usage = (800 - int(idle)) * 100 / 800
    return int(usage)


def cal_process_usage(data):
    usage = data.split()[8]
    usage = float(usage) * 100 / 800
    return int(usage)


class TopFrameParser(object):
    @staticmethod
    def parse_files(paths):
        sers = []
        for path in paths:
            with open(path, 'r') as f:
                lines = f.readlines()
                ser = TopFrameParser.parse_frame(lines)
                sers.append(ser)
        return sers

    @staticmethod
    def parse_frame(lines):
        dic = {}
        point = 7
        time = None

        for l in lines:
            if point == 0:
                break
            elif "TIME:" in l:
                time = l.split()[1]
                time = pd.to_datetime(time, format='%y%m%d%H%M%S')
                # time = datetime.strptime(time, '%y%m%d%H%M%S').date()
                point -= 1
            elif "%idle" in l:
                dic['cpu_usage'] = cal_cpu_usage(l)
                point -= 1
            elif "com.askey.dvr.d" in l:
                dic['dashcam'] = cal_process_usage(l)
                point -= 1
            elif "android.hardware.camera.provider" in l:
                dic['camera_provider'] = cal_process_usage(l)
            elif "media.codec" in l:
                dic['codec'] = cal_process_usage(l)
            elif "cameraserver" in l:
                dic['cameraserver'] = cal_process_usage(l)
            elif "top -b" in l:
                dic['top'] = cal_process_usage(l)
        ser = pd.Series(dic, name=time)
        return ser
